 package lingnan.cn.edu.view;


import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import lingnan.cn.edu.chat.ChatClient;
import lingnan.cn.edu.filter.Filter;
import lingnan.cn.edu.util.DateUtil;
import lingnan.cn.edu.util.FontUtil;
import lingnan.cn.edu.util.ShoppingCar;



public class MainView extends BaseView implements ActionListener, MouseListener{
	
	
	JMenuBar menuBar;
	JMenu settingMenu,helpMenu;
	
	JMenuItem helpMenuItem,configMenuItem;
	 
	CardLayout rightPanelLayout;
	JSplitPane containerPanel;
	JPanel leftPanel,rightPanel,upPanel,downPanel;
	
	JLabel logoLabel,Menu1Label,Menu2Label,Menu3Label,Menu4Label,logo,logo2;
	
	JPanel bottomPanel,shopPanel;
	
	JLabel timeLabel;
	//
	Timer timer;
	
	private String loginName;
	private static ShoppingCar shoppingCar = new ShoppingCar();
	
	Filter filter = new Filter();
	
	
	public MainView(String loginName) {
		super(1300,1000,"主界面"); 
		this.loginName = loginName;
		timer = new Timer(1000,this);
		timer.start();
	}
	
	public MainView() {
		
		super(1300,1000,"主界面"); 
		timer = new Timer(1000,this);
		timer.start();
	}
	

	@Override
	protected void initView() {
		
		
		menuBar = new JMenuBar();
		
		settingMenu = new JMenu("设置");
		
		helpMenu = new JMenu("帮助");
		
		helpMenuItem = new JMenuItem("联系客服",new ImageIcon("static\\icon\\setting.png"));
		helpMenuItem.addActionListener(this);
		
		configMenuItem = new JMenuItem("管理员界面",new ImageIcon("static\\icon\\setting.png"));
		configMenuItem.addActionListener(this);
		
		settingMenu.add(configMenuItem);
		helpMenu.add(helpMenuItem);
		
		menuBar.add(settingMenu);
		menuBar.add(helpMenu);
		
		setJMenuBar(menuBar);
		 
		
		
		leftPanel =new JPanel();
		
		leftPanel.setBackground(new Color(0xADD6DC));
		leftPanel.setLayout(null);
		leftPanel.setSize(1600, 1000);
		
		JLabel logoLabel = new JLabel(new ImageIcon("static\\img\\logo.png"));
		leftPanel.add(logoLabel);
		logoLabel.setBounds(50, 30, 200, 200);
		//奶茶系列
		Menu1Label = new JLabel("奶茶系列",new ImageIcon("static\\img\\1.png") ,JLabel.LEFT);
		Menu1Label.setBounds(60,200,300,150);
		Menu1Label.setFont(FontUtil.menuFont);
		Menu1Label.addMouseListener(this);
		
		leftPanel.add(Menu1Label);
		
		//果茶系列
		Menu2Label = new JLabel("果茶系列",new ImageIcon("static\\img\\2.png") ,JLabel.LEFT);
		Menu2Label.setBounds(60,300,300,150);
		Menu2Label.setFont(FontUtil.menuFont);
		Menu2Label.addMouseListener(this);
		leftPanel.add(Menu2Label);
		
		//牛奶气泡
		Menu3Label = new JLabel("牛奶冰沙",new ImageIcon("static\\img\\3.png") ,JLabel.LEFT);
		Menu3Label.setBounds(55,400,300,150);
		Menu3Label.setFont(FontUtil.menuFont);
		Menu3Label.addMouseListener(this);
		leftPanel.add(Menu3Label);
		//奶盖系列
		Menu4Label = new JLabel("奶盖系列",new ImageIcon("static\\img\\4.png") ,JLabel.LEFT);
		Menu4Label.setBounds(60,500,300,150);
		Menu4Label.setFont(FontUtil.menuFont);
		Menu4Label.addMouseListener(this);
		leftPanel.add(Menu4Label);
		
		logo = new JLabel(new ImageIcon("static\\img\\logo1.png"));
		logo.setBounds(-80, 750, 300, 150);
		logo2 = new JLabel(new ImageIcon("static\\img\\logo2.png"));
		logo2.setBounds(70, 600, 300, 150);
		leftPanel.add(logo);
		leftPanel.add(logo2);
		//选项卡
		rightPanelLayout = new CardLayout();
		//奶茶的列表
		JPanel Menu1Panel = new Menu1View();
		JPanel Menu2Panel = new Menu2View();
		JPanel Menu3Panel = new Menu3View();
		JPanel Menu4Panel = new Menu4View();
		Menu1Panel.setBackground(null);
		Menu2Panel.setBackground(null);
		Menu3Panel.setBackground(null);
		Menu4Panel.setBackground(null);
	
		rightPanel = new JPanel(rightPanelLayout);
		
		
		rightPanel.add(Menu1Panel, "0");
		rightPanel.add(Menu2Panel, "1");
		rightPanel.add(Menu3Panel, "2");
		rightPanel.add(Menu4Panel, "3");

		
		containerPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel);
		containerPanel.setDividerLocation(300);
		containerPanel.setDividerSize(0);
		
		
		
		bottomPanel = new JPanel();//默认的布局是流式布局
		bottomPanel.setBackground(new Color(0x58BAC8));
		bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		shopPanel = new JPanel();//默认的布局是流式布局
		 
		shopPanel.setBackground(new Color(0x58BfC8));
		shopPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		timeLabel = new JLabel(DateUtil.dateToString(new Date(),null));
		
		bottomPanel.add(timeLabel);
		Container container = getContentPane();
		container.add(containerPanel,"Center");
		container.add(bottomPanel,"South");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		timeLabel.setText(DateUtil.dateToString(new Date(),null));
		Object source = e.getSource();
		if(source==configMenuItem) {
			if(filter.filter(loginName)==1) {
				new ManageView(loginName);
				this.dispose();
			}else {
				JOptionPane.showMessageDialog(this, "您的权限不够喔，请使用管理员账号登录","提示",JOptionPane.ERROR_MESSAGE);
			}
				
		} else if (source == helpMenuItem) {
			// TODO 联系管理员
		/**/try {
				for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
					if ("Windows".equals(info.getName())) {
						UIManager.setLookAndFeel(info.getClassName());
						break;
					}
				}
			} catch (Exception exception) {
				exception.printStackTrace();
			}
			ChatClient client = new ChatClient();
			new Thread(client).start();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		Object source = e.getSource();
		if(source==Menu1Label) {
			rightPanelLayout.show(rightPanel,"0");
			Menu1View.countText.setText(String.valueOf(MainView.count));
		}
		if(source==Menu2Label) {
			rightPanelLayout.show(rightPanel,"1");
			Menu2View.countText.setText(String.valueOf(MainView.count));
		}
		if(source==Menu3Label) {
			rightPanelLayout.show(rightPanel,"2");
			Menu3View.countText.setText(String.valueOf(MainView.count));
		}
		if(source==Menu4Label) {
			rightPanelLayout.show(rightPanel,"3");
			Menu4View.countText.setText(String.valueOf(MainView.count));
		}
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		Object source = e.getSource();
		if(source==Menu1Label) {
			Menu1Label.setForeground(Color.white);
		}
		else if(source==Menu2Label) {
			Menu2Label.setForeground(Color.white);
		}
		else if(source==Menu3Label) {
			Menu3Label.setForeground(Color.white);
		}
		else if(source==Menu4Label) {
			Menu4Label.setForeground(Color.white);
		}
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		Object source = e.getSource();
		if(source==Menu1Label) {
			Menu1Label.setForeground(Color.black);
		}
		else if(source==Menu2Label) {
			Menu2Label.setForeground(Color.black);
		}
		else if(source==Menu3Label) {
			Menu3Label.setForeground(Color.black);
		}
		else if(source==Menu4Label) {
			Menu4Label.setForeground(Color.black);
		}
	}

	public static ShoppingCar getShoppingCar() {
		
		return shoppingCar;
	}
	
	public static int count = 0;
}
	